<template>
  <el-dialog :title="form.id ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible" :close-on-click-modal="false"
    @close="closeFn" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
      <el-row :gutter="24">
        <!-- <el-col :span="12" class="mb20">
          <el-form-item prop="uid" :label="$t('Video.uid')">
            <el-input v-model="form.uid" :placeholder="$t('Video.inputUidTip')" clearable show-word-limit />
          </el-form-item>
        </el-col> -->

        <el-col :span="12" class="mb20">
          <el-form-item prop="shopId" :label="$t('Video.shopId')">

            <ShopSelect @change="changeShopId"  v-model="form.shopId" ></ShopSelect>

            <!-- <el-select @change="changeShopId" v-model="form.shopId" :placeholder="$t('Prod.inputCategoryIdTip')" clearable
              filterable>
              <el-option :value="item.id" :label="item.id" v-for="item in shopListOption" :key="item!.id"></el-option>
            </el-select> -->
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item prop="topTime" :label="$t('Video.prodId')">

            <el-button @click="openSelect">{{ $t('Video.select') }}</el-button>
          </el-form-item>

          <div class="prod-table">
            <el-table :data="activeProdIdArr" border>
              <el-table-column prop="id" :label="$t('Video.prodId')" show-overflow-tooltip />
              <el-table-column prop="name" :label="$t('Video.prodName')" show-overflow-tooltip />
            </el-table>

          </div>
        </el-col>



        <el-col :span="12" class="mb20">
          <el-form-item prop="width" :label="$t('Video.width')">
            <el-input v-model="form.width" :placeholder="$t('Video.inputWidthTip')" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="height" :label="$t('Video.height')">
            <el-input v-model="form.height" :placeholder="$t('Video.inputHeightTip')" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="url" :label="$t('Video.url')">
            <el-input v-model="form.url" disabled :placeholder="$t('Video.inputUrlTip')" clearable show-word-limit
              :maxlength="255" />
          </el-form-item>
        </el-col>


        <el-col :span="12" class="mb20">
          <el-form-item prop="basePraiseCount" :label="$t('Video.uploadVideo')">

            <uploadVideo @changeFile="changeFileFn" :fileSize="50" v-model:imageUrl="form.url" :fileType="['video/mp4']"></uploadVideo>

          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item prop="desc" :label="$t('Video.desc')">
            <el-input v-model="form.desc" :placeholder="$t('Video.inputDescTip')" clearable show-word-limit
              :maxlength="500" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="type" :label="$t('Video.type')">
            <el-select v-model="form.type" :placeholder="$t('Video.inputTypeTip')" clearable filterable>
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_video_type"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="praiseCount" :label="$t('Video.praiseCount')">
            <el-input disabled v-model="form.praiseCount" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="commentCount" :label="$t('Video.commentCount')">
            <el-input disabled v-model="form.commentCount" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="favoriteCount" :label="$t('Video.favoriteCount')">
            <el-input disabled v-model="form.favoriteCount" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="shareCount" :label="$t('Video.shareCount')">
            <el-input disabled v-model="form.shareCount" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="playCount" :label="$t('Video.playCount')">
            <el-input disabled v-model="form.playCount" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="isSale" :label="$t('Video.isSale')">
            <el-switch v-model="form.isSale" :active-value="true" :inactive-value="false" />

            <!-- <el-input v-model="form.isSale" :placeholder="$t('Video.inputIsSaleTip')" clearable show-word-limit /> -->
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="isTop" :label="$t('Video.isTop')">

            <el-switch v-model="form.isTop" :active-value="true" :inactive-value="false" />

            <!-- <el-input v-model="form.isTop" :placeholder="$t('Video.inputIsTopTip')" clearable show-word-limit /> -->
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="state" :label="$t('Video.state')">
            <el-select v-model="form.state" :placeholder="$t('Video.inputStateTip')" clearable filterable>
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_video_state"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="baseFavoriteCount" :label="$t('Video.baseFavoriteCount')">
            <el-input v-model="form.baseFavoriteCount" :placeholder="$t('Video.inputBaseFavoriteCountTip')" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="basePraiseCount" :label="$t('Video.basePraiseCount')">
            <el-input v-model="form.basePraiseCount" :placeholder="$t('Video.inputBasePraiseCountTip')" clearable
              show-word-limit />
          </el-form-item>
        </el-col>



        <!-- <el-col :span="12" class="mb20">
          <el-form-item prop="topTime" :label="$t('Video.topTime')">
            <el-input v-model="form.topTime" :placeholder="$t('Video.inputTopTimeTip')" clearable show-word-limit />
          </el-form-item>
        </el-col> -->





      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
      </span>
    </template>
    <SelectVue ref="selsectREF" :getPage="getPage" :tableOption="[
      {
        prop: 'id', label: 'id'
      },
      {
        prop: 'name', label: $t('Video.prodName')
      }]" searchName="search" @submit="submitSelectFn" :shopId="(form.shopId as string)" />
  </el-dialog>
</template>

<script setup lang="ts" name="VideoDialog">
import { SubmitVideoForm, } from "./types/index"
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/bizs/Video'
const emit = defineEmits(['refresh']);
import { getPage } from "/@/api/bizs/Prod";

import uploadVideo from "/@/components/biz/uploadVideo.vue";

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
const ShopSelect = defineAsyncComponent(() => import("/@/components/ShopSelect/index.vue"))

// 定义字典
import { useDict } from '/@/hooks/dict';
const { biz_video_type, biz_video_state } = useDict('biz_video_type', 'biz_video_state')
const SelectVue = defineAsyncComponent(() => import("/@/components/ProdSelect/index.vue"))

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const dataFormRef = ref();
const activeProdIdArr = ref<{ id: string | number }[]>([]) //  选择的产品id
const selsectREF = ref()
// const shopListOption = ref<{ id: string | number }[]>([])

// 获取店铺list
// const getShopListFn = async () => {
//   let res = await getList()
//   shopListOption.value = res.data
// }
// getShopListFn()

const openSelect = () => {

  selsectREF.value.showFn(activeProdIdArr.value)
}



interface IChangeFileRespones {
  bucketName: string;
  fileName: string;
  url: string;
  videoFrameFileName: string;
  videoFrameUrl: string;
}

const changeFileFn = (data:IChangeFileRespones) => {
  form.frameUrl = data.videoFrameFileName
}


const changeShopId = () => {
  activeProdIdArr.value = []
}
const submitSelectFn = (selection: any) => {
  activeProdIdArr.value = selection
}
const closeFn = () => {
  activeProdIdArr.value = []
}
// 提交表单数据
const form = reactive<SubmitVideoForm>({
  id: '',
  uid: '',
  shopId: '',
  width: '',
  height: '',
  url: '',
  desc: '',
  type: '',
  praiseCount: '',
  commentCount: '',
  favoriteCount: '',
  shareCount: '',
  playCount: '',
  isSale: false,
  isTop: false,
  state: '',
  baseFavoriteCount: '',
  basePraiseCount: '',
  topTime: '',
  videoProdRecommendList: [],
  frameUrl:""
});

// 定义校验规则
// import { rule } from '/@/utils/validate';
const dataRules = ref({
  uid: [{ required: false, message: t('Video.inputUidTip'), trigger: 'blur' }],
  shopId: [{ required: false, message: t('Video.inputShopIdTip'), trigger: 'blur' }],
  width: [{ required: false, message: t('Video.inputWidthTip'), trigger: 'blur' }],
  height: [{ required: false, message: t('Video.inputHeightTip'), trigger: 'blur' }],
  url: [{ required: true, message: t('Video.inputUrlTip'), trigger: 'blur' }],
  desc: [{ required: false, message: t('Video.inputDescTip'), trigger: 'blur' }],
  type: [{ required: true, message: t('Video.inputTypeTip'), trigger: 'blur' }],
  praiseCount: [{ required: false, message: t('Video.inputPraiseCountTip'), trigger: 'blur' }],
  commentCount: [{ required: false, message: t('Video.inputCommentCountTip'), trigger: 'blur' }],
  favoriteCount: [{ required: false, message: t('Video.inputFavoriteCountTip'), trigger: 'blur' }],
  shareCount: [{ required: false, message: t('Video.inputShareCountTip'), trigger: 'blur' }],
  playCount: [{ required: false, message: t('Video.inputPlayCountTip'), trigger: 'blur' }],
  isSale: [{ required: false, message: t('Video.inputIsSaleTip'), trigger: 'blur' }],
  isTop: [{ required: false, message: t('Video.inputIsTopTip'), trigger: 'blur' }],
  state: [{ required: true, message: t('Video.inputStateTip'), trigger: 'blur' }],
  baseFavoriteCount: [{ required: false, message: t('Video.inputBaseFavoriteCountTip'), trigger: 'blur' }],
  basePraiseCount: [{ required: false, message: t('Video.inputBasePraiseCountTip'), trigger: 'blur' }],
  topTime: [{ required: false, message: t('Video.inputTopTimeTip'), trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
    form.frameUrl = ""
  });

  // 获取信息
  if (id) {
    form.id = id
    initFormData(id)
  }
};

// 初始化表单
const initFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
    activeProdIdArr.value = form.videoProdRecommendList!.map((el: any) => {
      return {
        id: el.prodId,
        name: el.prod.name
      }
    })

  })


};

// 提交表单
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.videoProdRecommendList = activeProdIdArr.value.map((el: any) => ({ prodId: el.id }))
    delete form.playCount
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('common.editSuccessText') : t('common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// const changeVideoUrl = (fileData: any) => {
//   form.url = fileData.fileName
// }


// 暴露变量
defineExpose({
  openDialog
});
</script>

<style lang="scss" scoped>
.prod-table {
  width: 80%;
  margin: auto;
}
</style>
